package interfacemanagement;

import indexer.DirectoryManager;
import java.io.IOException;
import config.ConfigManager;
import exception.InvalidConfigException;
import userinterface.TrayWindow;

public class MainWindowManager implements Receptor {
	private TrayWindow userInterface;
	private DirectoryManager manager;
	private static String DEFAULT_DIR;
	private long total;

	static {
		try {
			DEFAULT_DIR = ConfigManager.getStringConfig("default_dir");
		} catch (InvalidConfigException ex) {
			ex.printStackTrace();
			DEFAULT_DIR = "D:/Proyectos/HawarInDXR/prueba/";
		}
	}

	public static void main(String[] args) throws ClassNotFoundException, IOException {
		new MainWindowManager().initializeFileProcessing();
	}

	public MainWindowManager() throws ClassNotFoundException, IOException {
		this.manager = new DirectoryManager(DEFAULT_DIR, this);
	}

	public MainWindowManager(TrayWindow userInterface, String dir) throws ClassNotFoundException, IOException {
		this.manager = new DirectoryManager(DEFAULT_DIR, this);
		this.userInterface = userInterface;
	}

	public void initializeFileProcessing() {
		manager.start();
	}

	public void stopFileProcessing() {
		manager.setActive(false);
	}

	@Override
	public void notifyFinalization() {
		userInterface.notifySuccessfullyFinalization("Indexacion finalizada");
	}

	@Override
	public void notifyOnError(String error) {
		userInterface.notifyOnError("Error en Indexacion!");
	}

	@Override
	public void notifyAdvance(long advanceDirAmount, long advanceFileAmount, long totalFiles) {
		userInterface.notifyAdvance(advanceDirAmount, total, advanceFileAmount, totalFiles);
	}

	@Override
	public void setTotal(long total) {
		this.total = total;
	}
}
